package com.atguigu.spzx.manner.controller;

import com.atguigu.spzx.manner.service.SysRoleService;
import com.atguigu.spzx.model.dto.AssginRoleDto;
import com.atguigu.spzx.model.dto.SysRoleDto;
import com.atguigu.spzx.model.entity.SysRole;
import com.atguigu.spzx.model.myemun.ResultCodeEnum;
import com.atguigu.spzx.model.vo.common.Result;
import com.github.pagehelper.IPage;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/system/sysRole")
@CrossOrigin(allowCredentials = "true", originPatterns = "*", allowedHeaders = "*")
public class SysRoleController {
    @Autowired
    SysRoleService sysRoleService;

    @GetMapping("/findByPage/{pageNum}/{pageSize}")
    public Result<PageInfo<SysRole>> findByPage(@PathVariable("pageNum") Integer pageNum,
                                                @PathVariable("pageSize") Integer pageSize,
                                                SysRoleDto sysRoleDto) {
        PageInfo<SysRole>result= sysRoleService.findByPage(pageNum, pageSize, sysRoleDto);
        return Result.build(result, ResultCodeEnum.SUCCESS);
    }
    @PutMapping("/updateSysRole")
    public Result updateSysRole(@RequestBody SysRole sysRole) {
        sysRoleService.updateSysRole(sysRole);
        return Result.build(ResultCodeEnum.SUCCESS);
    }
    @PostMapping("/saveSysRole")
    public Result saveSysRole(@RequestBody SysRole sysRole) {
        sysRoleService.saveSysRole(sysRole);
        return Result.build(ResultCodeEnum.SUCCESS);
    }
    @DeleteMapping("/deleteById/{id}")
    public Result delete(@PathVariable("id") Long id) {
        sysRoleService.delete(id);
        return Result.build(ResultCodeEnum.SUCCESS);
    }

    @GetMapping("/findAllRoles/{id}")
    public Result<Map<String, Object> >findAllRoles(@PathVariable("id")Integer id) {
        List<SysRole> roles =sysRoleService.findAllRoles();
        List<Long> rolesById =sysRoleService.findAllRolesById(id);
        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
        stringObjectHashMap.put("allRolesList", roles);
        stringObjectHashMap.put("userRoleIds", rolesById);

        return Result.build(stringObjectHashMap, ResultCodeEnum.SUCCESS);
    }

}
